package com.hakim.wfb.customer.service.task;

import com.hakim.wfb.bean.*;
import com.hakim.wfb.biz.BenefitRecordBiz;
import com.hakim.wfb.customer.service.biz.StaticProfitBizService;
import com.hkfs.fundamental.spring.utils.SpringContextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/7/1.
 */
public class StaticProfitTask extends TimerTask {

    private static Logger logger = LoggerFactory.getLogger(StaticProfitTask.class);

    private StaticProfitBizService staticProfitBizService;

    private ConsumeRecord consumeRecord;

    private BenefitRecordBiz benefitRecordBiz;

    private Timer timer;

    private int canRunCount;

    public StaticProfitTask(Timer timer, int canRunCount, ConsumeRecord consumeRecord) {
        this.timer = timer;
        this.canRunCount = canRunCount;
        this.consumeRecord = consumeRecord;
    }

    @Override
    public void run() {

        benefitRecordBiz = (BenefitRecordBiz) SpringContextUtils.getBean("benefitRecordBiz");
        BenefitRecord benefitRecord = benefitRecordBiz.getBenefitRecord(consumeRecord.getTid());
        if (benefitRecord != null)
            return;

        logger.info("计算 uuid：" + consumeRecord.getCrUser() + "的静态收益。");
        staticProfitBizService = (StaticProfitBizService) SpringContextUtils.getBean("staticProfitBiz");
        Locker.locker.lock();
        try {
            staticProfitBizService.run(consumeRecord);
        } catch (Exception e) {

        } finally {
            Locker.locker.unlock();
        }
        canRunCount--;
        if (canRunCount == 0)
            timer.cancel();

        logger.info("计算完毕 uuid：" + consumeRecord.getCrUser() + "的静态收益。");
    }
}